package com.borball.store.comp;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.borball.store.domain.Customer;
import com.borball.store.domain.OrderItem;
import com.borball.store.domain.ProductAmount;
import com.borball.store.domain.ProductOrder;

public class SaleResultFrame extends JFrame{
	
	private static final long serialVersionUID = -4753383081215017673L;
	private Customer customer;
	private ProductOrder order;
	private List<OrderItem> items;
	private JTable table;
	private JTable table_1;
	private List<ProductAmount> list;
	
	SaleResultFrame(Customer c, ProductOrder order, List<OrderItem> items, List<ProductAmount> list, String logId){
		super();
		
		this.customer = c;
		this.order = order;
		this.items = items;
		this.list = list;
		
		final JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblHeader = new JLabel("销     售     单");
		lblHeader.setFont(new Font("幼圆", Font.BOLD, 12));
		lblHeader.setBounds(225, 25, 145, 35);
		panel.add(lblHeader);
		
		JLabel labelOrderID = new JLabel("订单编号：" + order.getId());
		labelOrderID.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelOrderID.setBounds(20, 70, 140, 23);
		panel.add(labelOrderID);
		
		JLabel labelCustomer = new JLabel("客    户：" + order.getCustomer());
		labelCustomer.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelCustomer.setBounds(180, 70, 220, 23);
		panel.add(labelCustomer);
		
		JLabel labelMoney = new JLabel("金    额：" + order.getTotal() + "(已付:" + order.getBack() + ")");
		labelMoney.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelMoney.setBounds(420, 70, 160, 23);
		panel.add(labelMoney);
		
//		panel.add(table);
		
		JLabel labelPAmount = new JLabel("商品数量：" + getAmount());
		labelPAmount.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelPAmount.setBounds(420, 90, 160, 23);
		panel.add(labelPAmount);
		
		JLabel labelMobile = new JLabel("客户电话：" + customer.getTele());
		labelMobile.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelMobile.setBounds(20, 90, 140, 23);
		panel.add(labelMobile);
		
		JLabel labelAddress = new JLabel("送货地址：" + customer.getAddress());
		labelAddress.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelAddress.setBounds(180, 90, 220, 23);
		panel.add(labelAddress);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(list2Array(),
			new String[] {
				"产品名称", "单价", "数量", "小计"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, Float.class, Integer.class, Float.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.getColumnModel().getColumn(0).setPreferredWidth(220);
		table.getColumnModel().getColumn(0).setMinWidth(100);
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(1).setMinWidth(100);
		table.getColumnModel().getColumn(2).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setMinWidth(100);
		table.getColumnModel().getColumn(3).setPreferredWidth(130);
		table.getColumnModel().getColumn(3).setMinWidth(100);
		table.setFont(new Font("幼圆", Font.PLAIN, 10));
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(20, 125, 550, 250);
		panel.add(scrollPane);
		
		JLabel labelCustomerSign = new JLabel("客户签名：_________________");
		labelCustomerSign.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelCustomerSign.setBounds(20, 385, 160, 23);
		panel.add(labelCustomerSign);
		
		JLabel labelSonghuoSign = new JLabel("销售签名：_________________");
		labelSonghuoSign.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelSonghuoSign.setBounds(200, 385, 160, 23);
		panel.add(labelSonghuoSign);
		
		JLabel labelDate = new JLabel("日期：_________________");
		labelDate.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelDate.setBounds(400, 385, 180, 23);
		panel.add(labelDate);
		
		JLabel label = new JLabel("出     货     单");
		label.setFont(new Font("幼圆", Font.BOLD, 12));
		label.setBounds(225, 450, 145, 35);
		panel.add(label);
		
		JLabel label_1 = new JLabel("订单编号：" + order.getId());
		label_1.setFont(new Font("幼圆", Font.PLAIN, 10));
		label_1.setBounds(20, 490, 140, 23);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("出仓流水：" + logId);
		label_2.setFont(new Font("幼圆", Font.PLAIN, 10));
		label_2.setBounds(170, 490, 140, 23);
		panel.add(label_2);
		
		table_1 = new JTable();
		table_1.setModel(new DefaultTableModel(getOutDatas(),
			new String[] {"电脑板名称", "数量" }
		) {
			Class[] columnTypes = new Class[] {
				String.class, Integer.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table_1.getColumnModel().getColumn(0).setPreferredWidth(200);
		table_1.getColumnModel().getColumn(0).setMinWidth(100);
		table_1.getColumnModel().getColumn(1).setPreferredWidth(100);
		table_1.getColumnModel().getColumn(1).setMinWidth(100);
	
		table_1.setFont(new Font("幼圆", Font.PLAIN, 10));
		JScrollPane scrollPane_1 = new JScrollPane(table_1);
		scrollPane_1.setBounds(20, 515, 558, 160);
		panel.add(scrollPane_1);
		
		JLabel labelJingshouSign = new JLabel("经手人：_________________");
		labelJingshouSign.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelJingshouSign.setBounds(20, 680, 220, 23);
		panel.add(labelJingshouSign);
		
		JLabel labelDate2 = new JLabel("日期：_________________");
		labelDate2.setFont(new Font("幼圆", Font.PLAIN, 10));
		labelDate2.setBounds(300, 680, 250, 23);
		panel.add(labelDate2);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(270, 715, 250, 40);
		
		getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JButton btnPrint = new JButton("打     印");
		panel_1.add(btnPrint);
		btnPrint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PrinterJob printJob = PrinterJob.getPrinterJob();
				PageFormat pageFormat = printJob.defaultPage(); // 获取默认打印页面格式
				
				Paper paper = new Paper();
				paper.setSize(595,26f/2.45f*72f);    //设置A4纸  
				paper.setImageableArea(0f,0f,595,26f/2.45f*72f);
				 
//			    double margin = 36; // half inch
//			    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
//			        - margin * 2);
			    pageFormat.setPaper(paper);

				Printable printable = new Printable() {

					public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
						if (pageIndex == 1)
							return Printable.NO_SUCH_PAGE;
						panel.printAll(graphics);
						return Printable.PAGE_EXISTS;
					}
				};
				printJob.setPrintable(printable, pageFormat); // 设置打印工作
				try {
					printJob.print();
				} catch (PrinterException e1) {
					System.out.println("错误：" + e1);
				}

			
			}
		});
		btnPrint.setFont(new Font("幼圆", Font.BOLD, 12));
		
		this.getContentPane().setPreferredSize(new Dimension(595, 760));
		
		pack();
		setLocationRelativeTo(null);
		
	}
	

	private Object[][] getOutDatas() {
		Object[][] o = new Object[list.size()][2];
		
		int i = 0;
		for (ProductAmount pa : list) {
			o[i][0] = pa.getName();
			o[i][1] = pa.getAmount();
			i++;
		}
		
		return o;
	}

	Object[][] list2Array(){
		Object[][] o = new Object[items.size()][4];
		
		int i = 0;
		for (OrderItem orderItem : items) {
			o[i][0] = orderItem.getName();
			o[i][1] = orderItem.getPrice();
			o[i][2] = orderItem.getAmount();
			o[i][3] = orderItem.getTotal();
			i++;
		}
		
		return o;
	}
	
	int getAmount(){
		int amount = 0;
		for (OrderItem orderItem : items) {
			amount += orderItem.getAmount();
		}
		
		return amount;
	}
	
	public static void main(String[] args) {
		Customer c = new Customer();
		c.setName("客户1");
		c.setTele("138999999");
		c.setAddress("地址1111111111111111111");
		
		ProductOrder order = new ProductOrder();
		order.setId(100);
		order.setCustomer("客户1");
		order.setTotal(20000f);
		order.setCreated(new Timestamp(System.currentTimeMillis()));
		
		List<OrderItem> items = new ArrayList<OrderItem>();		
		OrderItem item1 = new OrderItem();
		item1.setName("产品1");
		item1.setPrice(1000f);
		item1.setAmount(10);
		item1.setTotal(10000f);
		
		items.add(item1);
		
		OrderItem item2 = new OrderItem();
		item2.setName("产品2");
		item2.setPrice(1000f);
		item2.setAmount(10);
		item2.setTotal(10000f);
		
		items.add(item2);
		
		SaleResultFrame frame = new SaleResultFrame(c, order, items, null, "12423456");
		
		frame.setSize(600,800);
		frame.setVisible(true);
	}
}
